﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Contract.SaleOut.Record;
using DataCenter.Data;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using Common.Contract;
using Common.Util;
using System.Data.Entity;

namespace DataCenter.Provider
{
    public class DataCenterProvider : GenericProvider, IDataCenterProvider
    {
        public List<DataCenters> MultiSearchReport(string fromDate = null, string toDate = null, string shopCode = null, string product = null, string model = null, string regionId = null, string yearMonth = null, string year = null, string billtoCode = null)
        {
            var query = QueryDataCenters();
            if (!string.IsNullOrEmpty(fromDate))
            {
                var frDate = fromDate.ToDateTime();
                query = query.Where(x => x.SelloutDate > frDate);
            }
            if (!string.IsNullOrEmpty(toDate))
            {
                var toDa = toDate.ToDateTime();
                query = query.Where(x => x.SelloutDate < toDa);
            }
            if (!string.IsNullOrEmpty(shopCode))
            {
                query = query.Where(x => x.ShopCode.Contains(shopCode));
            }
            if (!string.IsNullOrEmpty(product))
            {
                query = query.Where(x => x.ModelCat.ProdL1.Contains(product));
            }
            if (!string.IsNullOrEmpty(model))
            {
                query = query.Where(x => x.Model.Contains(model));
            }
            if (!string.IsNullOrEmpty(regionId))
            {
                query = query.Where(x => x.ShopInf.RegId.Equals(regionId));
            }
            if (!string.IsNullOrEmpty(yearMonth))
            {

            }
            if (!string.IsNullOrEmpty(billtoCode))
            {
                query = query.Where(x => x.ShopInf.BilltoCode.Equals(billtoCode));
            }
            if (!string.IsNullOrEmpty(year))
            {

            }
            return query.ToList();
        }
        public List<DataCenters> SearchReport(string textSearch, string fromDate = null, string toDate = null)
        {
            var query = QueryDataCenters();
            if (!string.IsNullOrWhiteSpace(textSearch))
            {
                query = query.Where(x => x.ShopCode.ToUpper().Contains(textSearch.ToUpper())
                    || x.Model.ToUpper().Contains(textSearch.ToUpper())
                    || x.ShopInf.RegId.ToUpper().Equals(textSearch.ToUpper()));
            }
            if (!string.IsNullOrEmpty(fromDate))
            {
                var frDate = fromDate.ToDateTime();
                query = query.Where(x => x.SelloutDate > frDate);
            }
            if (!string.IsNullOrEmpty(toDate))
            {
                var toDa = toDate.ToDateTime();
                query = query.Where(x => x.SelloutDate < toDa);
            }
            return query.ToList();
        }
        public IQueryable<DataCenters> QueryDataCenters()
        {
            var data = repository.DbContext as DataCenterEntities;
            var query = from x in data.DataCenters.Include(x => x.ShopInf)//.Include(x => x.ModelCat)
                        select x;
            return query;
        }


        public List<DataCenters> SearchMonthlyReport(string product, string model, string regionId, string year, string billtoCode)
        {
            throw new NotImplementedException();
        }

        public List<DataCenters> SearchDailyReport(string fromDate = null, string toDate = null, string shopCode = null, string product = null, string model = null, string regionId = null)
        {
            throw new NotImplementedException();
        }

        public List<DataCenters> SearchWeeklyReport()
        {
            throw new NotImplementedException();
        }


        public List<SaleIn> SearchSaleIn(string partCheck)
        {
            int partCheckVal = partCheck.ToInt();
            var querryCdc = repository.GetQuery<SerialNumberCdcHist>();
            if (!partCheck.IsNullOrWhiteSpace())
            {
                querryCdc = querryCdc.Where(x => Convert.ToInt32(x.SerialNo.Substring(1, 3)) >= partCheckVal);
            }

            var querryRdc = repository.GetQuery<SerialNumberRdcHist>();
            if (!partCheck.IsNullOrWhiteSpace())
            {
                querryRdc = querryRdc.Where(x => Convert.ToInt32(x.SerialNo.Substring(1, 3)) >= partCheckVal);
            }
            //var dictCdc = querryCdc.Select(x=>x.SerialNo).ToDictionary(x=>x.SerialNo);
            //var dictRdc = querryRdc.ToDictionary(x=>x.SerialNo);

            //var result = querryCdc.Select(x => new SaleIn { 
            //    BilltoCode = x.BilltoCode,
            //    BilltoName = x.BilltoName,
            //    Model = x.Model,
            //    //SerialNo = x.SerialNo
            //    Suffix = x.Suffix
            //}).ToList();
            //return result;

            return null;
        }
    }
}
